﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
//using System.Windows.Interop;

namespace Bricks.BaseFramework.GenericForms
{
    /// <summary>
    /// MsgBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MsgBoxWindow : Window
    {
        MessageBoxResult mbResult = MessageBoxResult.None;

        //IntPtr ActiveWindowHandle;  //定义活动窗体的句柄

        //[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        //public static extern IntPtr GetActiveWindow();  //获得父窗体句柄

        public MsgBoxWindow()
        {
            InitializeComponent();  
        }

        #region 窗体操作
        private void gridTop_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        #endregion

        #region ShowCore基础方法
        public MessageBoxResult ShowCore(Window owner, string message, string title, MessageBoxButton buttons, MessageBoxImage image)
        {
            //若owner赋予值为null
            if (owner == null)
            {
                //ActiveWindowHandle = GetActiveWindow();  //获取父窗体句柄
                //WindowInteropHelper helper = new WindowInteropHelper(this);
                //helper.Owner = ActiveWindowHandle;

                //将Owner指定为主窗口
                //this.Owner = Application.Current.MainWindow;

                //将Owner指定为当前活动窗口
                foreach (Window w in Application.Current.Windows)
                {
                    if (w.IsActive)
                    {
                        this.Owner = w;
                        break;
                    }
                }
            }
            else
            {
                this.Owner = owner;
            }
            txtMessage.Text = message;
            txtTitle.Text = title;
            SetImage(image);
            SetButton(buttons);
            this.ShowDialog();
            return mbResult;
        }
        #endregion

        #region 图片和按钮的定义
        /// <summary>
        /// 提示图片的定义
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private void SetImage(MessageBoxImage image)
        {
            switch (image)
            {
                case MessageBoxImage.Error:
                    imgIcon.Source = new BitmapImage(new Uri("/BaseFramework;component/Images/MsgBox/msg_error.png", UriKind.Relative)); 
                    break;
                case MessageBoxImage.Exclamation:
                    imgIcon.Source = new BitmapImage(new Uri("/BaseFramework;component/Images/MsgBox/msg_exclamation.png", UriKind.Relative)); 
                    break;
                case MessageBoxImage.Information:
                    imgIcon.Source = new BitmapImage(new Uri("/BaseFramework;component/Images/MsgBox/msg_info.png", UriKind.Relative)); 
                    break;
                case MessageBoxImage.Question:
                    imgIcon.Source = new BitmapImage(new Uri("/BaseFramework;component/Images/MsgBox/msg_question.png", UriKind.Relative)); 
                    break;
                default:
                    imgIcon.Source = new BitmapImage(new Uri("/BaseFramework;component/Images/MsgBox/msg_info.png", UriKind.Relative)); 
                    break;
            }
        }

        /// <summary>
        /// 提示按钮的定义：已添加焦点的选中
        /// </summary>
        /// <param name="buttons"></param>
        private void SetButton(MessageBoxButton buttons)
        {
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    btnOK.Visibility = Visibility.Visible;
                    btnOK.Focus();
                    break;
                case MessageBoxButton.OKCancel:
                    btnOK.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Visible;
                    btnOK.Focus();
                    break;
                case MessageBoxButton.YesNo:
                    btnYes.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    btnYes.Focus();
                    break;
                case MessageBoxButton.YesNoCancel:
                    btnYes.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Visible;
                    btnYes.Focus();
                    break;
                default: 
                    btnOK.Visibility = Visibility.Visible;
                    btnOK.Focus();
                    break;
            }
        }
        #endregion

        #region 按钮事件
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            mbResult = MessageBoxResult.OK;
            this.DialogResult = true;            
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            mbResult = MessageBoxResult.Yes;
            this.DialogResult = true;
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            mbResult = MessageBoxResult.No;
            this.DialogResult = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            mbResult = MessageBoxResult.Cancel;
            this.DialogResult = false;
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Y)
            {
                mbResult = MessageBoxResult.Yes;
                this.DialogResult = true;
            }
            if (e.Key == Key.N)
            {
                mbResult = MessageBoxResult.No;
                this.DialogResult = false;
            }
        }
        #endregion
    }
}
